<template>
	<view class="wrap">
	  <button class="parsing" @click="upload">上传</button>
	  <uni-section title="我的文件列表" type="line"></uni-section>
	  <uni-list>
		  <uni-list-item :ellipsis="1" @click="toEesc(file)" v-for="file in fileList" :title="file.name" :note="getTime(file.time)" showArrow 
		  :thumb="file.type=='图片'?file.url:null" thumb-size="lg" :rightText="file.type" />
		</uni-list>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				fileList: [],
				fileUrl: 'false'
			}
		},
		async onLoad(e) {
			await this.$onLaunched
			this.getMyFile();
			//#ifdef MP
			wx.showShareMenu({
			  withShareTicket: true,
			  menus: ['shareAppMessage', 'shareTimeline']
			})
			//#endif
		},
		methods: {
			getTime(time){
				//#ifdef H5
				return (new Date(parseInt(time))).toLocaleDateString()
				//#endif
				//#ifdef MP
				return (new Date(parseInt(time*1000))).toLocaleDateString()
				//#endif
			},
			getMyFile(){
				this.$api('myfile',"get").then(res=>{
					this.fileList=res.data
				})
			},
			upload(){
				this.chooseAndUploadFile()
			},
			getFileSize(size){
				let temp=size/1000/1000
				if(temp<1){
					return (temp*1000).toFixed(2)+'KB'
				}else{
					return temp.toFixed(2)+'MB'
				}
			},
			toEesc(data){
				uni.navigateTo({
					url:'filedesc?data='+JSON.stringify(data)
				})
			},
			getFileType(name){
			   // var imageFilter=".jpeg|.gif|.jpg|.png|.bmp|.pic|.svg|"
			   // let videoFilter="avi|wmv|mpg|mpeg|mov|rm|ram|swf|flv|mp4|mp3|wma|avi|rm|rmvb|flv|mpg|mkv|"
			   // if(name.indexOf(".")>-1){
				  //  console.log(name)
				  //  var p = name.lastIndexOf(".")
				  //  var strPostfix=name.substring(p,name.length) + '|'
				  //  strPostfix = strPostfix.toLowerCase()
				  //  if(imageFilter.indexOf(strPostfix)>-1){
					 //   return '图片'
				  //  }else if(videoFilter.indexOf(strPostfix)>-1){
					 //   return '视频'
				  //  }else{
					 //   return '文件'
				  //  }
			   // }
			   // return '文件'
			   if(name=='video'){
				   return "视频"
			   }else if(name=='image'){
				   return "图片"
			   }else{
				   return "文件"
			   }
			},
			async chooseAndUploadFile(){
				let res=await uniCloud.chooseAndUploadFile({type: 'all'})
				uni.showLoading({
					title:'上传中。。。'
				})
				for(let r of res.tempFiles){
					console.log(r)
					// r typeof===File
					//#ifdef H5
					let data={}//JSON.parse(JSON.stringify(r))
					data.cloudPath=r.cloudPath
					data.time=r.lastModified
					data.lastModifiedDate=r.lastModifiedDate
					data.name=r.name
					data.size=r.size
					data.url=r.url
					data.type=r.type.split("/")[0]
					data.size=this.getFileSize(data.size)
					data.type=this.getFileType(data.type)
					await this.$api('myfile',"add",data)
					this.fileList.push(data)
					//#endif
					//#ifdef MP
					r.size=this.getFileSize(r.size)
					r.type=this.getFileType(r.type)
					await this.$api('myfile',"add",r)
					this.fileList.push(r)
					//#endif
					
				}
				uni.hideLoading()
				uni.showToast({
					icon:'success',
					title:"上传成功"
				})
			}
		}
	}
</script>

<style>
	page {
	    background-color: #0000;
	    height: 100%;
	    font-size: 32rpx;
	    line-height: 1.6;
	}
	.wrap{
		padding-bottom: 60px;
	}
	.parsing-btn-hover {
	    background: #3f6ddd;
	    border: 1px solid #1e4ecb;
	}
	.parsing {
	    overflow: hidden;
	    display: block;
	    width: 664rpx;
	    height: 88rpx;
	    line-height: 88rpx;
	    color: #fff;
	    background-color: #337aff;
	    box-shadow: 0 14rpx 28rpx rgba(51,122,255,0.3);
	}
</style>

